<template>
  <j-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="封面">
          <j-image-upload text="上传" v-decorator="['cover', { rules: [ { required: true, message: '请上传封面!' },]}]" ></j-image-upload>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="标题">
          <a-input placeholder="请输入标题" v-decorator="['title', { rules: [ { required: true, message: '请输入标题!' },]}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="内容">
          <j-editor ref="editor" placeholder="请输入内容" v-decorator="['content', { rules: [ { required: true, message: '请输入内容!' },]}]" :triggerChange=true />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="类型">
          <j-dict-select-tag v-decorator="['type', { rules: [ { required: true, message: '请选择类型!' },] }]"
                             title="类型" :triggerChange=true dictCode="informationType" placeholder="请选择类型" />
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import JImageUpload from '@/components/jeecg/JImageUpload'
import JUpload from '@/components/jeecg/JUpload'
import JEditor from '@comp/jeecg/JEditor.vue'

export default {
  name: "IndustryInformationModel",
  components: { JEditor, JImageUpload,JUpload},
  data () {
    return {
      title:"操作",
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 2 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 },
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules:{
        createUser:{rules: [{ required: true, message: '请输入更新人!' }]},
        updateUser:{rules: [{ required: true, message: '请输入更新人!' }]},
      },
      url: {
        add: "/mpMini/carinformation/add",
        edit: "/mpMini/carinformation/edit",
      },
    }
  },
  created () {
  },
  methods: {
    add () {
      this.edit({});
    },
    edit (record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model,'title','content','type','view','cover'))
        //时间格式化
      });

    },
    close () {
      this.$emit('close');
      this.visible = false;
    },
    handleOk () {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if(!this.model.id){
            httpurl+=this.url.add;
            method = 'post';
          }else{
            httpurl+=this.url.edit;
            method = 'put';
          }
          let formData = Object.assign(this.model, values);
          //时间格式化

          console.log(formData)
          httpAction(httpurl,formData,method).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.close();
          })
        }
      })
    },
    handleCancel () {
      this.close()
    },


  }
}
</script>

<style lang="less" scoped>

</style>